#pragma once

#include <QWidget>
#include <qpainter.h>
#include "ui_showWidget.h"
#include "arbitrage.h"
#include "MGraph.h"
class Result {
public:
    double max_product;
    QVector<int> max_path;
};
class showWidget : public QWidget
{
	Q_OBJECT

public:
	showWidget(QWidget *parent = nullptr);
	~showWidget();
    Ui::showWidgetClass ui;
    MGraph m;
    bool arr[25][25] = {false};
    bool key = false;

    // 绘制圆
    void myPaint(QPainter& painter, int x, int y, quint8 n);
    // 绘制箭头
    void paintArrow(QPainter& painter, QPointF start, QPointF end,QPen pen,int,int, QPointF(* a)[25]);
    // 强调
    void highlight(QPainter& painter, QPointF start, QPointF end, QPen pen, int,int,QPointF(* a)[25]);
    //显示数值
    void drawNum(QPainter& painter, QPointF start, QPointF end,int i,int j, int(*a)[25], QPointF(*interPoints)[25]);
    // DFS
    void dfs( QVector<bool>& visited, QVector<int>& path, int start, int current, double product, Result& result,int& index,bool &flag);
    // 绘图
    void paintEvent(QPaintEvent* paintEvent) {
        // 设置大小
        this->setGeometry(150, 75, 1200, 800);
        // 获取中心点
        int X = this->geometry().width() / 2-200;
        int Y = this->geometry().height() / 2;
        QPainter painter(this);
        // 设置抗锯齿
        painter.setRenderHint(QPainter::Antialiasing);
        // 设置颜色不同的五种Pen
        QPen pens[5];
        pens[0].setColor(QColor(0, 0, 0));
        pens[1].setColor(QColor(255, 0, 0));
        pens[2].setColor(QColor(0, 255, 0));
        pens[3].setColor(QColor(0, 0, 255));
        pens[4].setColor(QColor(255, 255, 255));
        // 绘制中心点
        painter.setPen(pens[1]);
        painter.drawPoint(QPoint(X, Y));
        // 绘制圆
        painter.setPen(pens[0]);
        qDebug() << this->m.getVexNum();
        myPaint(painter, X, Y, this->m.getVexNum());
        // 获取个点的位置
        QPointF points[25];
        QPointF interPoints[25][25];
        
        for (int i = 0; i < this->m.getVexNum(); i++) {
            points[i] = QPointF(300 * cos(2 * i * M_PI / this->m.getVexNum()), 300 * sin(2 * i * M_PI / this->m.getVexNum()));
           // painter.drawText(QPointF(350 * cos(2 * i * M_PI / num), 350 * sin(2 * i * M_PI / num)), currency[i]);
        }
        for (int i = 0; i < this->m.getVexNum(); i++) {
            for (int j = i + 1; j < this->m.getVexNum(); j++) {
                double dx = points[i].x() - points[j].x();
                double dy = points[i].y() - points[j].y();
                double length = sqrt(dx * dx + dy * dy);
                double offset = 50;
                QPointF normal(dy / length * offset, -dx / length * offset);
                interPoints[i][j] = normal;
                interPoints[j][i] = -normal;
            }
        }
        int location[25][25];
        for (int i = 0; i < this->m.getVexNum(); i++) {
            for (int j = i + 1; j < this->m.getVexNum(); j++) {
                location[i][j] = 1;
                location[j][i] = -1;
            }
        }
        if (!key) {
            for (int i = 0; i < this->m.getVexNum(); i++) {
                for (int j = 0; j < this->m.getVexNum(); j++) {
                    if (i == j)    continue;
                    if (this->m.edge[i][j] > 0) {
                        paintArrow(painter, points[(i+1)% this->m.getVexNum()], points[(j + 1) % this->m.getVexNum()], pens[2], (i + 1) % this->m.getVexNum(), (j + 1) % this->m.getVexNum(), interPoints);
                        
                    }
                }
            }
        }
        else {
            for (int i = 0; i < this->m.getVexNum(); i++) {
                for (int j = 0; j < this->m.getVexNum(); j++) {
                    if (i == j)    continue;
                    if (arr[i][j] == true && this->m.edge[i][j] > 0) {
                        //paintArrow(painter, points[i], points[j], pens[2]);
                         highlight(painter, points[(i+1)% this->m.getVexNum()], points[(j + 1) % this->m.getVexNum()],pens[1], (i + 1) % this->m.getVexNum(), (j + 1) % this->m.getVexNum(), interPoints);
                         drawNum(painter, points[(i + 1) % this->m.getVexNum()], points[(j + 1) % this->m.getVexNum()], i, j,location, interPoints);
                    }
                    /*else {
                         paintArrow(painter, points[i], points[j],pens[2]);
                     }*/
                }
            }
        }
    }

private slots:
    void outlineWay();
    void showAllWay();
    void back();
    void find();
private:
    
};
